package boddaert.philippe.cashGame.site.deuxfoismoincher;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import au.id.jericho.lib.html.Element;
import au.id.jericho.lib.html.Source;
import boddaert.philippe.cashGame.Site;
import boddaert.philippe.cashGame.parser.game.IWebGameParser;
import boddaert.philippe.cashGame.parser.game.WebGameParserFactory;
import boddaert.philippe.cashGame.site.WebGame;
import boddaert.philippe.cashGame.site.WebPage;

public class WebPageParser extends
		boddaert.philippe.cashGame.parser.page.WebPageParser {

	private final static Logger logger = Logger.getLogger(WebPageParser.class);

	@Override
	protected List<WebGame> extractGamesList(WebPage page) {
		List<Element> findAllElements = null;
		List<WebGame> games = new ArrayList<WebGame>();

		Source source = new Source(page.getContent());
		List<Element> Elements = source.findAllElements("div");
		Element rootOfGamesList = null;

		for (Element element : Elements) {
			if ("liste_pdts".equals(element.getAttributeValue("id")))
				rootOfGamesList = element;
		}

		if (rootOfGamesList != null)
			findAllElements = rootOfGamesList.findAllElements("div");

		for (Element element : findAllElements) {
			if ("produit".equals(element.getAttributeValue("class"))) {
				games.add(new WebGame(element));
			}
		}
		if (games.size() == 0)
			return null;
		
		return games;
	}

	public IWebGameParser getGameParser() {
		return WebGameParserFactory.get(Site.DEUX_FOIS_MOINS_CHER);
	}

}
